package com.larsgard.gedviewer.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.larsgard.gedviewer.client.event.EventBus;
import com.larsgard.gedviewer.client.event.ProcessGedFileFinishedEvent;
import com.larsgard.gedviewer.client.event.ProcessGedFileFinishedEvent.ProcessGedFileFinishedHandler;
import com.larsgard.gedviewer.client.service.UserDataService;
import com.larsgard.gedviewer.client.service.UserDataServiceAsync;
import com.larsgard.gedviewer.client.service.callback.ProcessGedFileCallback;
import com.larsgard.gedviewer.shared.ProcessRequest;
import com.larsgard.gedviewer.shared.ProcessResult;
import com.larsgard.gedviewer.shared.ProcessRequest.OPERATION;
import com.larsgard.gedviewer.shared.entities.UserTree;

public class UserTreeItem extends Composite implements ProcessGedFileFinishedHandler {

    private UserDataServiceAsync dataservice = GWT.create(UserDataService.class);

    private static UserTreeItemUiBinder uiBinder = GWT.create(UserTreeItemUiBinder.class);

    interface UserTreeItemUiBinder extends UiBinder<Widget, UserTreeItem> {
    }

    @UiField
    Label createdDate, personsInTree, processed;

    @UiField
    HTML link;

    @UiField
    Button delete;

    @UiField
    Button process;

    private UserTree userTree;

    public UserTreeItem() {
	initWidget(uiBinder.createAndBindUi(this));
	EventBus.getInstance().addHandler(ProcessGedFileFinishedEvent.TYPE, this);
    }

    public UserTreeItem(UserTree tree) {
	this();
	this.setUserTree(tree);
    }

    @UiHandler("process")
    public void processGedFile(ClickEvent event) {
	ProcessRequest request = new ProcessRequest();
	request.setOperation(OPERATION.PROCESS_GED);
	request.setFileKey(userTree.getGedFileId());
	link.setText("Processing... please do not leave this page.");
	dataservice.processGedFile(request, new ProcessGedFileCallback());
    }

    @UiHandler("delete")
    public void deleteTree(ClickEvent event) {
	ProcessRequest request = new ProcessRequest();
	request.setOperation(OPERATION.DELETE);
	request.setFileKey(userTree.getGedFileId());
	dataservice.processGedFile(request, new ProcessGedFileCallback());
    }

    private void initFields(UserTree tree) {
	if (tree == null) {
	    link.setHTML("Link");
	    processed.setText("Processed");
	    delete.setVisible(false);
	    process.setVisible(false);
	} else {
	    String url = createUrl(tree.getPassword());
	    link.setHTML("<a href=\"" + url + "\"> " + url + "</a>");
	    link.setWordWrap(true);
	    createdDate.setText(tree.getCreatedDate().toString());
	    if (tree.getProcessed()) {
		process.setEnabled(false);
		processed.setText("Yes");
	    } else {
		process.setEnabled(true);
		processed.setText("No");
	    }
	}
    }

    private String createUrl(String pwd) {
	String href = Window.Location.getHref();
	String param = href.contains("?") ? "&pwd=" : "?pwd=";
	return href + param + pwd;
    }

    public void setUserTree(UserTree userTree) {
	this.userTree = userTree;
	initFields(userTree);
    }

    public UserTree getUserTree() {
	return userTree;
    }

    @Override
    public void onGedFileProcessed(ProcessGedFileFinishedEvent event) {
	ProcessResult result = event.getResult();
	if (userTree != null //
		&& result.getOperation() == OPERATION.PROCESS_GED //
		&& result.getFileId().equals(userTree.getGedFileId())) {
	    if (result.isFinished()) {
		initFields(userTree);
		process.setEnabled(false);
	    } else {
		link.setText("Processed " + result.getPosition() + " of " + result.getTotal()
			+ " please wait...");
		ProcessRequest request = new ProcessRequest();
		request.setSkipCount(result.getPosition());
		request.setOperation(OPERATION.PROCESS_GED);
		request.setFileKey(userTree.getGedFileId());
		dataservice.processGedFile(request, new ProcessGedFileCallback());
	    }
	}
    }

}
